<?php
class Registers_Controller extends Template_Controller {

	public $template = 'client/index';
	
	
    public function __construct()
    {
        parent::__construct();
        
        //Css
        $this->template->css = $this->site['theme_url'].'home/home.css';
        //View
        //$this->get_view();
		$this->language = $this->get_client_lang();
    }
    
    public function index()
    {	
       $this->template->content = new View('registers/index');
	   $model = new Programs_Model(); 

		$mPrograms = $model->get_with_active($this->language);

		$this->template->content->mPrograms= $mPrograms;
    }
	
    public function thanks()
    {
    	$this->template->content = new View('registers/thanks');
    }
	public function save()

    {

    	$record = $this->_get_record();
    	
    	if($record)
    	{
    		$this->send_email($record);
    	}
    	
    	url::redirect('contact');
		die();	

    }
	 private function _get_record()

    {
		
		
    	$form = array
	    (
	        'txt_name' => '',
	        'txt_designation' => '',
	        'txt_email' => '',
	        'txt_subject' => '',
			'chk_program' => '',
			
			'selGender' => '',	
			
			'txt_organization'=>'',
			'txt_address'=>'',
			'txt_phone'=>'',
			'txt_giamdoc'=>'',
			'txt_chucdanh'=>'',	
			'txt_email_congty'=>'',				
	    );
		
    	$errors = $form;
		
		if($_POST)
    	{
    		$post = new Validation($_POST);
    		
			$post->pre_filter('trim', TRUE);
			/*$post->add_rules('txt_name','required');
			$post->add_rules('txt_email','required','email');
			$post->add_rules('txt_subject','required');
			$post->add_rules('txt_content','required');
			//$post->add_callbacks('txt_random',array($this,'_check_security_code'));
			$post->add_rules('sel_send','trim');*/
			
			if($post->validate())
 			{
 				$form = arr::overwrite($form, $post->as_array());
				
 				return $form;
			} else {
				$errors = arr::overwrite($errors, $post->errors('contact_validation'));
				$error_msg = '';
				foreach($errors as $id => $name) 
					if($name) $error_msg .= '<br>'.$name;
				$this->session->set_flash('error_msg',$error_msg);
				
				url::redirect('contact');
				die();
			}
        }
    }
	private function send_email($record)
    {
    	
		//print_r($record);
		$listPorgram = $record['chk_program'];
		$name = $record['txt_name'];
		$email = $record['txt_email'];
		$Gender = $record['selGender'];
		$designation = $record['txt_designation'];
		
		$id='';
		foreach($listPorgram as $list){
		$id .=$list.',';
		
		}
		$id = substr($id,0,-1);
		$model = new Programs_Model(); 
		$this->db->in('programs.programs_id',$id);
		$mPrograms = $model->get_with_active($this->language);

		$html_content = "<table width='99%' border='1' cellspacing='0' cellpadding='5' style='border-collapse:collapse'>
	  <tr>
		<td colspan='2' class='title_special' >". Kohana::lang('registers_lang.lbl_program')."</td>
		<td width='23%' class='title_special'>".  Kohana::lang('registers_lang.lbl_program_schedule')."</td>
		<td width='11%' class='title_special'>".  Kohana::lang('registers_lang.lbl_program_investment')."</td>
	  </tr>	  ";
	  $i=0;
	  foreach($mPrograms as $list){ 
	  $i++;
	  $html_content .=  "
	  <tr>
		<td width='5%' valign='top'>".$i."</td>
		<td width='61%' valign='top'>".  $list['programs_title']."</td>
		<td valign='top'>".  date('d/m/Y',$list['programs_begin_date'])."</td>
		<td>&nbsp;</td>
	  </tr>";
	  } 
	 
	 $html_content .=  "</table>";
	 $html_content .= "<table  width='99%' border='1' cellspacing='0' cellpadding='5' style='border-collapse:collapse'>
		  <tr>
			<td colspan='2' class='title_special'>". Kohana::lang('registers_lang.lbl_program_student')."</td>
			<td width='15%' align='center' class='title_special'>". Kohana::lang('registers_lang.lbl_gender')."</td>
			<td width='18%' align='center' class='title_special'>". Kohana::lang('registers_lang.lbl_designation')."</td>
			<td width='17%' align='center' class='title_special'>". Kohana::lang('registers_lang.lbl_your_email')."</td>
		  </tr>";
		  $j=1;
		 for($i=0;$i<count($name);$i++){ 
		 if(!empty($name[$i])){
		  $html_content .=" <tr>
		    <td width='4%'>".$j."</td>
		    <td width='46%'>".$name[$i]." </td>
		    <td>".$Gender[$i]."</td>
		    <td>".$designation[$i]."</td>
		    <td>".$email[$i]."</td>
	      </tr>
		 ";
		 $j++;
		 	}
		}
		$html_content.= "</table>";
		
		$html_content .="<table width='100%' border='0' cellspacing='0' cellpadding='2'>
  <tr>
    <td width='19%'>&nbsp;</td>
    <td width='81%'>&nbsp;</td>
  </tr>
  <tr>
    <td colspan='2'><strong>".Kohana::lang('registers_lang.lbl_program_info_company')."</strong></td>
  </tr>
  <tr>
    <td>". Kohana::lang('registers_lang.lbl_organization')."</td>
    <td>".$record['txt_organization']."</td>
  </tr>
  <tr>
    <td>". Kohana::lang('registers_lang.lbl_street_address')."</td>
    <td>".$record['txt_address']."</td>
  </tr>
  <tr>
    <td>". Kohana::lang('registers_lang.lbl_phone')."</td>
    <td>".$record['txt_phone']."</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
   <tr>
    <td colspan='2'><strong>". Kohana::lang('registers_lang.lbl_program_dieukhoan')."</strong></td>
  </tr>
 
  <tr>
    <td>". Kohana::lang('registers_lang.lbl_program_giamdoc')."</td>
    <td>".$record['txt_giamdoc']."</td>
  </tr>
  <tr>
    <td>". Kohana::lang('registers_lang.lbl_program_chucdanh')."</td>
    <td>".$record['txt_chucdanh']."</td>
  </tr>
  <tr>
    <td>Email</td>
    <td>".$record['txt_email_congty']."</td>
  </tr>
</table>";
	 ///echo $html_content;
		//die();
		//Use connect() method to load Swiftmailer
		$swift = email::connect();
		 
		//From, subject
		$from = $record['txt_email_congty'];
		$subject = 'Dang ky lop hoc ';
		
		//HTML message
		/*$path = 'application/views/email_tpl/contact.tpl';
		$fi = fopen($path,'r+');
		$html_content = file_get_contents($path); 	
		//Replate content	
		$html_content = str_replace('#contact_name#',$record['txt_name'],$html_content);		
		$html_content = str_replace('#contact_phone#',$record['txt_phone'],$html_content);		
		$html_content = str_replace('#contact_content#',$record['txt_content'],$html_content);	
		fclose($fi);
*/
		//Build recipient lists
		$to='ncongdong@gmail.com';
		$recipients = new Swift_RecipientList;
		$recipients->addTo($to);
	
		 
		//Build the HTML message
		$message = new Swift_Message($subject, $html_content, "text/html");
		 
		if($swift->send($message, $recipients, $from)){
			url::redirect('registers/thanks');
			die();
		} else {
			
		}
		// Disconnect
		$swift->disconnect();
    }
}
?>